//
//(C) Copyright 2011 HongKong Airline.,Ltd. 
//
/**
 * publishNewsDaoHibernate
 *
 * 项目名称:代购系统
 *
 * @author Mengxiangliang
 *
 * 功能描述:
 * 
 * 历史版本:2013-01-04 v1.0.0 Mengxiangliang  创建:
 * 
 */
package com.gamutsoft.amb.publishnews.dao.impl;

import java.util.List;
import java.util.Map;

import com.gamutsoft.amb.common.bean.QueryExample;
import com.gamutsoft.amb.common.constants.Constants;
import com.gamutsoft.amb.common.dao.impl.GenericDaoHibernate;
import com.gamutsoft.amb.common.exception.BusinessException;
import com.gamutsoft.amb.publishnews.dao.IPublishNewsDao;
import com.gamutsoft.amb.publishnews.model.PublishNews;

@SuppressWarnings("unchecked")
public class PublishNewsDaoHibernate extends GenericDaoHibernate<PublishNews, Long> implements IPublishNewsDao {
    
     public PublishNewsDaoHibernate() {
		super(PublishNews.class);

	}
    
    /**
	 * 根据条件查询记录
	 * @author Mengxiangliang
	 * @param  QueryExample
	 * @return List<Map>
	 * @throws BusinessException
	 */
	@Override
	public List<Map> searchPublishNewsList(QueryExample qe) throws BusinessException {
	
	    StringBuffer sb = new StringBuffer();
		sb.append("SELECT T.ID,T.CODE,T.TITLE,T.CONTENT,T.STATUS,T.TYPE,T.URL,T.ORDER_NUM,T.CLASS_TYPE FROM PUBLISH_NEWS T");
		//qe.addOrderBy("");
		return this.findByQueryExample(sb.toString(), qe);
		
	}

	/**
	 * 根据ID查询记录
	 * @author Mengxiangliang
	 * @param id
	 * @return
	 * @throws BusinessException
	 */
	@Override
	public PublishNews searchPublishNewsById(Long id) throws BusinessException {
	
	  PublishNews domain = new PublishNews();
	   String hql = "from PublishNews where id=?";
	   List<PublishNews> list = this.getHibernateTemplate().find(hql, id);
	   if(list != null && list.size() >0){
		  domain = list.get(0);
	   }
	   return domain;
		
	}

	/**
	 * 删除记录
	 * @author Mengxiangliang
	 * @param id
	 * @throws BusinessException
	 */
	@Override
	public void deletePublishNewsById(Long id) throws BusinessException {
	
	 String hql ="delete from PublishNews where id=?";
	 this.getHibernateTemplate().bulkUpdate(hql, id);
	 
	}

	@Override
	public List<Map> searchPublishNewsByWhere(QueryExample qe,
			PublishNews domain) throws BusinessException {
		 StringBuffer sb = new StringBuffer();
			sb.append("SELECT T.ID,T.CODE,T.TITLE,T.CONTENT,T.STATUS,T.TYPE,T.URL,T.ORDER_NUM,T.CLASS_TYPE FROM PUBLISH_NEWS T");
			if(domain!=null){
				if(domain.getClass()!=null && !"".equals(domain.getClass())){
					qe.addCondictions("T.CLASS_TYPE","=",domain.getClass());
				}
				if(domain.getTitle()!=null && !"".equals(domain.getTitle())){
					qe.addCondictions("T.TITLE","=",domain.getTitle());
				}
				if(domain.getStatus()!=null && !"".equals(domain.getStatus())){
					qe.addCondictions("T.STATUS","=",domain.getStatus());
				}
				
			}
			
			qe.addOrderBy(" T.ORDER_NUM desc");
		 return this.findByQueryExample(sb.toString(), qe);
	}

	@Override
	public PublishNews searchPublishNewsByClass(String classCode)
			throws BusinessException {
		  PublishNews domain = null;
		   String hql = "from PublishNews where classType=? and status=?";
		   List<PublishNews> list = this.getHibernateTemplate().find(hql, classCode,Constants.STATE_VALID);
		   if(list != null && list.size() >0){
			  domain = list.get(0);
		   }
		   return domain;
	}
	
}